//
//  ConfirmVC.m
//  Doctor
//
//  Created by 阳光 on 16/9/12.
//  Copyright © 2016年 阳光. All rights reserved.
//  认证界面

#import "ConfirmVC.h"
#import "JCTagView.h"
#import "SuPhotoPicker.h"
#import "CountryListVC.h"
#import "AreaListVC.h"
#import "HospitalListVC.h"
#import "JobFromYearVC.h"
#import "ReviewVC.h"

#import "XWDragCellCollectionView.h"
#import "AddHospitalCell.h"
#import "AppDelegate.h"

#import "SelectTimeYearsView.h"
#import "SelectSexView.h"

#define addHospitalBtnH  30
#define addHospitalBtnW  100



@interface ConfirmVC ()<UIScrollViewDelegate,UITextViewDelegate,UITextFieldDelegate,UIActionSheetDelegate,JobFromYearVCDelegate,JCTagViewDelegate,XWDragCellCollectionViewDataSource,XWDragCellCollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,CustomNavViewDelegate,SelectTimeYearsViewDelegate,SelectSexViewDelegate>

@property(nonatomic,strong)CustomBtn *addHospitalBtn;

@property(nonatomic,strong)NSString *hospitalStr;

@property(nonatomic,strong)NSMutableArray *hospitalArr;

@property(nonatomic,strong)NSMutableArray *hospitalIDArr;

@property(nonatomic,strong)NSMutableArray *areaIDArr;

@property(nonatomic,strong)XWDragCellCollectionView *collectionView;

@property(nonatomic,strong)UIScrollView *scrollView;

// 头像
@property(nonatomic,strong)UIImageView *headUrl;

@property(nonatomic,strong)Label_TextField_Next *nameView;


@property(nonatomic,strong)UIView *sexView;

@property(nonatomic,strong)Label_TextField_Next *emailView;

@property(nonatomic,strong)UIView *timeView;

@property(nonatomic,strong)UIButton *selectTimeBtn;

@property(nonatomic,strong)UIView *countryAreaView;

@property(nonatomic,strong)UIView *hospitalAreaView;

@property(nonatomic,strong)UIView *jobHospitalView;

@property(nonatomic,strong)ImageTextButton *selectAreaBtn;

@property (nonatomic, strong) NSArray *tagsArray;

@property (nonatomic, strong) NSMutableArray *tagsFrames;

// View
@property (nonatomic, strong) JCTagView *JCView;

@property(nonatomic,strong)UIView *mainView;

@property(nonatomic,strong)Label_TextField_Next *codeView;

@property(nonatomic,strong)NSString *countryID;

@property(nonatomic,strong)NSString *areaID;

@property(nonatomic,strong)NSString *hospitalID;

@property(nonatomic,strong)UILabel *selectTimeLabel;

@property(nonatomic,strong)UILabel *selectHospitalAreaLabel;

@property(nonatomic,strong)UILabel *selectCountryAreaLabel;

@property(nonatomic,strong)UILabel *selectJobHospitalLabel;

@property(nonatomic,strong)UITextView *profileView;

@property(nonatomic,strong)NSMutableArray *selectTitleArr;

@property(nonatomic,strong)NSString *selectTitleStr1;

@property(nonatomic,strong)NSString *selectTitleStr2;

@property(nonatomic,strong)SelectTimeYearsView *selectTimeYearsView;

@property(nonatomic,strong)SelectSexView *selectSexView;

@end




@implementation ConfirmVC

-(void)rightBtnEvent
{
    
}

-(void)selectTitleWithArr:(NSMutableArray *)titleArr index:(NSInteger)indexStr
{
    if (indexStr==100000)
    {
        _selectTitleStr1=[[titleArr componentsJoinedByString:@","] stringByAppendingString:@","];
    }
    else
    {
        _selectTitleStr2=[[titleArr componentsJoinedByString:@","] stringByAppendingString:@","];
    }
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _hospitalArr=[NSMutableArray array];
    
    _hospitalIDArr=[NSMutableArray array];
    
    _areaIDArr=[NSMutableArray array];
    
    _selectTitleArr=[NSMutableArray array];
    
    self.view.backgroundColor=WhiteColor;
    
    [self initScrollView];
    
    // 注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:) name:@"tongzhi" object:nil];
    
    
    UITapGestureRecognizer *tapGestureRecognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
    tapGestureRecognizer.cancelsTouchesInView=NO;
    // 将触摸事件添加到当前view
    [self.view addGestureRecognizer:tapGestureRecognizer];
    
}

-(void)keyboardHide:(UITapGestureRecognizer*)tap
{
    [_nameView.textField resignFirstResponder];
    [_emailView.textField resignFirstResponder];
    [_codeView.textField resignFirstResponder];
    [_profileView resignFirstResponder];

}


// UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([@"\n" isEqualToString:text]==YES)
    {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}


-(void)tongzhi:(NSNotification *)text
{
    [_hospitalArr addObject:text.userInfo[@"hospitalName"]];
    
    _hospitalStr=text.userInfo[@"hospitalName"];

    [_hospitalIDArr addObject:text.userInfo[@"hospitalID"]];
    
    [_areaIDArr addObject:text.userInfo[@"areaID"]];
    
    _hospitalID=[[_hospitalIDArr componentsJoinedByString:@","] stringByAppendingString:@","];
    
    [_collectionView reloadData];
    
    [self resetLayout];
    
}

-(void)initScrollView
{
    _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,APPW,APPH)];
    _scrollView.backgroundColor=WhiteColor;
    _scrollView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_scrollView];
    
    _mainView=[self setMainView];
    setViewCoord_Y(_mainView,0);
    [_scrollView addSubview:_mainView];
    _scrollView.contentSize=CGSizeMake(APPW,CGRectGetMaxY(self.mainView.frame)+kmargin);
    
}

-(NSArray *)dataSourceArrayOfCollectionView:(XWDragCellCollectionView *)collectionView
{
    return self.hospitalArr;
}

-(void)dragCellCollectionView:(XWDragCellCollectionView *)collectionView newDataArrayAfterMove:(NSMutableArray *)newDataArray
{
    [self.hospitalArr removeAllObjects];
    [self.hospitalArr addObjectsFromArray:newDataArray];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.hospitalArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AddHospitalCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"AddHospitalCell" forIndexPath:indexPath];
    [cell.hospitalBtn setTitle:self.hospitalArr[indexPath.row] forState:0];
    [cell.hospitalBtn setTitleColor:WhiteColor forState:0];
    cell.hospitalBtn.titleLabel.font=Font_Number(13);
    cell.hospitalBtn.titleLabel.lineBreakMode=NSLineBreakByTruncatingTail;
    cell.hospitalBtn.backgroundColor=Main_Color;
    cell.deleteBtn.tag=100+indexPath.row;
    [cell.deleteBtn addTarget:self action:@selector(deleteBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}


#pragma mark --UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((APPW-40)/3,(APPW-40)/3);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(kmargin,kmargin,0,kmargin);
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

-(void)deleteBtnEvent:(UIButton *)sender
{
    [self.hospitalArr removeObjectAtIndex:sender.tag-100];
    [self resetLayout];
    
}

-(void)resetLayout
{
    int columnCount = ceilf((self.hospitalArr.count + 1)/3);
    
    float height=columnCount*(addHospitalBtnH+10)+10;
    if (height < addHospitalBtnH+20)
    {
        height =addHospitalBtnH+20;
    }
    CGRect rect = _collectionView.frame;
    rect.size.height=height;
    _collectionView.frame=rect;
    [_collectionView reloadData];
    
    
    self.addHospitalBtn.frame=CGRectMake(10+(10+(APPW-40)/3)*(self.hospitalArr.count%3),CGRectGetHeight(self.collectionView.frame)-addHospitalBtnH-10,(APPW-40)/3,addHospitalBtnH);
}

-(void)selectBtnEvent
{
    _selectTimeYearsView=[[SelectTimeYearsView alloc]initWithFrame:CGRectMake(0,0,APPW,APPH)];
    _selectTimeYearsView.delegate=self;
    [_selectTimeYearsView addTarget:self action:@selector(dismissShowView) forControlEvents:UIControlEventTouchUpInside];
     [[UIApplication sharedApplication].keyWindow addSubview:_selectTimeYearsView];
}

// SelectTimeYearsViewDelegate
-(void)selectTimeYearWithIndex:(NSString *)index timeYear:(NSString *)timeYear
{
     [_selectTimeYearsView removeFromSuperview];
    
    UIButton *timeBtn=(UIButton *)[self.view viewWithTag:123];
    [timeBtn setTitle:timeYear forState:0];
}

// SelectSexViewDelegate
-(void)selectSexWithIndex:(NSInteger)index sex:(NSString *)sex
{
    [_selectSexView removeFromSuperview];
    UIButton *sexBtn=(UIButton *)[self.view viewWithTag:124];
    [sexBtn setTitle:sex forState:0];
}

-(void)dismissShowView
{
    [_selectTimeYearsView removeFromSuperview];
}

-(void)leftBtnEvent
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

-(void)sexBtnEvent
{
    _selectSexView=[[SelectSexView alloc]initWithFrame:CGRectMake(0,0,APPW,APPH)];
    _selectSexView.delegate=self;
    [_selectSexView addTarget:self action:@selector(dismissShowSexView) forControlEvents:UIControlEventTouchUpInside];
    [[UIApplication sharedApplication].keyWindow addSubview:_selectSexView];
}

// UITextViewDelegate
-(void)textViewDidChange:(UITextView *)textView
{
    NSString *countStr=[NSString stringWithFormat:@"%d",MaxTextInputValue-textView.text.length];
    NSInteger countNumber=[countStr integerValue];
     UILabel *countLabel=(UILabel *)[self.view viewWithTag:666];
    if (countNumber>0)
    {
         countLabel.text=[NSString stringWithFormat:@"%@/%d",countStr,MaxTextInputValue];
    }
   
    if (textView.text.length>MaxTextInputValue)
    {
        textView.text=[textView.text substringToIndex:MaxTextInputValue];
        [self.view makeToast:@"输入内容超过最大限制" duration:.1 position:CSToastPositionCenter];
         countLabel.text=[NSString stringWithFormat:@"0/%d",MaxTextInputValue];
    }
}

-(void)dismissShowSexView
{
    [_selectSexView removeFromSuperview];
}

-(UIView *)setMainView
{
    UIView *mainView=[[UIView alloc]initWithFrame:CGRectMake(0,0,APPW,0)];
    mainView.backgroundColor=WhiteColor;

    CustomNavView *loginNavView=[[CustomNavView alloc]initWithTitle:NSLocalizedString(@"Verification", nil) leftBtnImage:@"back_gray" hasLeftBtnImage:YES rightBtnTitle:nil hasRightBtnTitle:NO];//@"认证"
//    CustomNavView *loginNavView=[[CustomNavView alloc]initWithTitle:@"认证" leftBtnImage:@"back_gray" hasLeftBtnImage:YES rightBtnTitle:nil hasRightBtnTitle:NO];
    loginNavView.delegate=self;
    loginNavView.frame=CGRectMake(0,0,APPW,64);
    [self.view addSubview:loginNavView];
    
    setViewHeight_Add(mainView,64+kmargin);
    
    // 头像
    UIImageView *headUrl=[[UIImageView alloc]initWithFrame:CGRectMake((APPW-128/2)/2,mainView.frame.size.height,128/2,128/2)];
    ViewBorderRadius(headUrl,128/4,2,ClearColor);
    [mainView addSubview:headUrl];
    headUrl.tag=100;
    headUrl.image=[UIImage imageNamed:@"head"];
    setViewHeight_Add(mainView,128/2+kmargin);
    
    // 上传头像
    UIButton *uploadHeadUrlBtn=[[UIButton alloc]initWithFrame:CGRectMake((APPW-100)/2,mainView.frame.size.height,100,30)];
    [mainView addSubview:uploadHeadUrlBtn];
    uploadHeadUrlBtn.backgroundColor=WhiteColor;
    uploadHeadUrlBtn.titleLabel.font=Font_Number(16);
    ViewBorderRadius(uploadHeadUrlBtn,kmargin/2,1,Color979797);
    [uploadHeadUrlBtn setTitle:NSLocalizedString(@"Header photo", nil) forState:UIControlStateNormal];//@"上传头像"//[uploadHeadUrlBtn setTitle:@"上传头像" forState:UIControlStateNormal];
    [uploadHeadUrlBtn setTitleColor:Color666666 forState:UIControlStateNormal];
    [uploadHeadUrlBtn addTarget:self action:@selector(uploadHeadUrlBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    setViewHeight_Add(mainView,30+kmargin+1);
    
    UIView *headLine=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,1)];
    [mainView addSubview:headLine];
    headLine.backgroundColor=MAIN_BACK_COLOR;
    setViewHeight_Add(mainView,1);
    
    // 姓名
    {
         _nameView=[[Label_TextField_Next alloc]initWithTitle:NSLocalizedString(@"Name", nil) placeholder:NSLocalizedString(@"Enter name", nil)];//_nameView=[[Label_TextField_Next alloc]initWithTitle:@"姓名" placeholder:@"请填写姓名"];
        _nameView.frame=CGRectMake(0,mainView.frame.size.height,APPW,kmargin*5);
        _nameView.backgroundColor=WhiteColor;
        _nameView.textField.textAlignment=NSTextAlignmentRight;
        [mainView addSubview:_nameView];
        setViewHeight_Add(mainView,kmargin*5);
        
        UIView *nameLine=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,1)];
        [mainView addSubview:nameLine];
        nameLine.backgroundColor=MAIN_BACK_COLOR;
        setViewHeight_Add(mainView,1);
    }
    
    // 性别
    {
        
        _sexView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,kmargin*5)];
        [mainView addSubview:_sexView];
        _sexView.backgroundColor=WhiteColor;
        
        UILabel *sexLabel=[_sexView getLabelWithText:NSLocalizedString(@"Gender", nil) font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        sexLabel.frame=CGRectMake(kmargin,0,APPW/2,kmargin*5);
        [_sexView addSubview:sexLabel];
        
        
        UIButton *sexBtn=[UIButton new];
        sexBtn.tag=124;
        [_sexView addSubview:sexBtn];
        sexBtn.titleLabel.textAlignment=NSTextAlignmentRight;
        [sexBtn addTarget:self action:@selector(sexBtnEvent) forControlEvents:UIControlEventTouchUpInside];
        [sexBtn setTitle:NSLocalizedString(@"Select gender", nil) forState:0];//@"选择性别"
        [sexBtn setTitleColor:ColorBDBDBD forState:0];
        sexBtn.titleLabel.font=Font_Number(16);
        [sexBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-kmargin);
            make.height.mas_equalTo(kmargin*3);
            make.centerY.mas_equalTo(sexLabel.mas_centerY);
        }];
        
        
        UIView *sexLine=[UIView new];
        [_sexView addSubview:sexLine];
        sexLine.backgroundColor=MAIN_BACK_COLOR;
        [sexLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(APPW,1));
            make.bottom.mas_equalTo(0);
        }];
        
        setViewHeight_Add(mainView,kmargin*5);
    }
    
    // Email
    {
        _emailView=[[Label_TextField_Next alloc]initWithTitle:NSLocalizedString(@"Mail", nil) placeholder:NSLocalizedString(@"Enter mail", nil)];//@"邮箱" @"请填写邮箱";
        _emailView.frame=CGRectMake(0,mainView.frame.size.height,APPW,kmargin*5);
        _emailView.textField.keyboardType=UIKeyboardTypeEmailAddress;
        _emailView.backgroundColor=WhiteColor;
        _emailView.textField.textAlignment=NSTextAlignmentRight;
        [mainView addSubview:_emailView];
        setViewHeight_Add(mainView,kmargin*5);
        
        UIView *emailLine=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,1)];
        [mainView addSubview:emailLine];
        emailLine.backgroundColor=MAIN_BACK_COLOR;
        setViewHeight_Add(mainView,1);
    }
    
    
    // 从业年份
    {
        _timeView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,kmargin*5)];
        [mainView addSubview:_timeView];
        _timeView.backgroundColor=WhiteColor;
        
         UILabel *timeLabel=[_timeView getLabelWithText:NSLocalizedString(@"Working Years", nil) font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];//"从业年份"
        timeLabel.frame=CGRectMake(kmargin,0,APPW/2,kmargin*5);
        [_timeView addSubview:timeLabel];
        
        UIView *selectView=[UIView new];
        [_timeView addSubview:selectView];
        [selectView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(timeLabel.mas_right).offset(kmargin);
            make.centerY.mas_equalTo(timeLabel.mas_centerY);
            make.right.mas_equalTo(0);
            make.top.mas_equalTo(timeLabel.mas_top);
        }];
        
        UIButton *selectBtn=[UIButton new];
        selectBtn.tag=123;
        [_timeView addSubview:selectBtn];
        selectBtn.titleLabel.textAlignment=NSTextAlignmentRight;
        [selectBtn addTarget:self action:@selector(selectBtnEvent) forControlEvents:UIControlEventTouchUpInside];
        [selectBtn setTitle:NSLocalizedString(@"Working for years", nil) forState:0];//@"选择从业时间"
        [selectBtn setTitleColor:ColorBDBDBD forState:0];
        selectBtn.titleLabel.font=Font_Number(16);
        [selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-kmargin);
            make.height.mas_equalTo(kmargin*3);
            make.centerY.mas_equalTo(timeLabel.mas_centerY);
        }];
        
        
        UIView *timeLine=[UIView new];
        [_timeView addSubview:timeLine];
        timeLine.backgroundColor=MAIN_BACK_COLOR;
        [timeLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(APPW,1));
            make.bottom.mas_equalTo(0);
        }];
        
        setViewHeight_Add(mainView,kmargin*5);
    }
    
    // 就职医院
    {
        _jobHospitalView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,kmargin*10)];
        [mainView addSubview:_jobHospitalView];
        _jobHospitalView.backgroundColor=WhiteColor;
        
        // 就职医院
        UILabel *jobHospitalLabel=[_jobHospitalView getLabelWithText:NSLocalizedString(@"Clinic", nil) font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];//@"就职医院"
        [_jobHospitalView addSubview:jobHospitalLabel];
        jobHospitalLabel.frame=CGRectMake(kmargin,kmargin,APPW/2,kmargin*2);
        
        
        
        UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init];
        _collectionView=[[XWDragCellCollectionView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(jobHospitalLabel.frame),APPW,addHospitalBtnH+kmargin*2) collectionViewLayout:flowLayout];
        _collectionView.dataSource=self;
        _collectionView.delegate=self;
        _collectionView.scrollEnabled=NO;
        _collectionView.backgroundColor=WhiteColor;
        [_collectionView registerClass:[AddHospitalCell class] forCellWithReuseIdentifier:@"AddHospitalCell"];
        [_jobHospitalView addSubview:_collectionView];
        
        CustomBtn *addHospitalBtn=[[CustomBtn alloc]initWithFrame:CGRectMake(kmargin,kmargin,addHospitalBtnW,addHospitalBtnH)];
        [addHospitalBtn setTitleColor:Color666666 forState:0];
        addHospitalBtn.titleLabel.textAlignment=NSTextAlignmentCenter;
        addHospitalBtn.titleLabel.font=Font_Number(14);
        [_collectionView addSubview:addHospitalBtn];
        ViewBorderRadius(addHospitalBtn,kmargin/5,1,ColorBDBDBD);
        [addHospitalBtn setTitle:NSLocalizedString(@"AddHospital", nil) forState:0];//@"添加医院"
        [addHospitalBtn setImage:[UIImage imageNamed:@"add"] forState:0];
        [addHospitalBtn setImage:CGRectMake(kmargin,(addHospitalBtnH-kmargin*3/2)/2,kmargin*3/2,kmargin*3/2) title:CGRectMake(kmargin*2,0,addHospitalBtnW-kmargin*2,addHospitalBtnH)];
        [addHospitalBtn addTarget:self action:@selector(addHospitalBtnEvent) forControlEvents:UIControlEventTouchUpInside];
        _addHospitalBtn=addHospitalBtn;
        
        setViewHeight_Add(mainView,CGRectGetMaxY(self.collectionView.frame));
    }
    
    // 职称
     UILabel *jobLabel=[mainView getLabelWithText:NSLocalizedString(@"Title", nil) font:Font_Number(16) textColor:Color333333 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];//@"职称"
    [mainView addSubview:jobLabel];
    jobLabel.frame=CGRectMake(kmargin,mainView.frame.size.height+kmargin,APPW,30);
    setViewHeight_Add(mainView,30+kmargin);
    
    NSArray *jobArr=@[NSLocalizedString(@"Clinical Director", nil),NSLocalizedString(@"Deputy Clinical Director", nil),NSLocalizedString(@"Professor", nil),NSLocalizedString(@"Associate Professor", nil),@"Physician",NSLocalizedString(@"PhD", nil),NSLocalizedString(@"MD", nil),NSLocalizedString(@"HCLD", nil),NSLocalizedString(@"FACOG", nil)];
    //    NSArray *jobArr=@[@"主任医师",@"副主任医师",@"教授",@"副教授",@"医师",@"博士",@"医学博士",@"高难临床实验室主任",@"美国妇产科学院协会会员"];
    
    JCTagView *JCView=[[JCTagView alloc] initWithFrame:CGRectMake(0,mainView.frame.size.height+kmargin/2,APPW,0)];
    JCView.tag=100000;
    [mainView addSubview:JCView];
    JCView.delegate=self;
    JCView.JCSignalTagColor=WhiteColor;
    [JCView setArrayTagWithLabelArray:jobArr];
    
    setViewHeight_Add(mainView,JCView.frame.size.height+kmargin);
    
    UIView *jobLine=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,1)];
    [mainView addSubview:jobLine];
    jobLine.backgroundColor=MAIN_BACK_COLOR;
    setViewHeight_Add(mainView,1);
    
    // 专业擅长
    UILabel *skillLabel=[mainView getLabelWithText:NSLocalizedString(@"Speciality", nil) font:Font_Number(16) textColor:Color333333 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];//@"专业擅长"
    [mainView addSubview:skillLabel];
    skillLabel.frame=CGRectMake(kmargin,mainView.frame.size.height+kmargin,APPW,30);
    setViewHeight_Add(mainView,30+kmargin);
    
    
    NSArray *skillArr=@[NSLocalizedString(@"Laparoscope", nil),NSLocalizedString(@"Hysteroscope", nil),NSLocalizedString(@"Endometriosis", nil),NSLocalizedString(@"POF", nil),NSLocalizedString(@"Ovulation Induction", nil),NSLocalizedString(@"Assisted Reproduction", nil),NSLocalizedString(@"In Vitro Fertilization", nil),NSLocalizedString(@"Ovulation Failure", nil),NSLocalizedString(@"PCOS", nil),NSLocalizedString(@"Tubal Obstruction", nil),NSLocalizedString(@"Recurrent Pregnancy Loss", nil),NSLocalizedString(@"Andrology", nil),NSLocalizedString(@"Infertility", nil)];
    //     NSArray *skillArr=@[@"腹腔镜",@"宫腔镜",@"子宫内膜异位",@"卵巢早衰",@"促排卵",@"辅助生殖",@"试管婴儿",@"排卵障碍",@"多囊卵巢",@"输卵管不通",@"习惯性流产",@"男科不育",@"不孕不育"];

    
    JCTagView *JCView1=[[JCTagView alloc] initWithFrame:CGRectMake(0,mainView.frame.size.height+kmargin/2,APPW,0)];
    JCView1.tag=100001;
    JCView1.delegate=self;
    [mainView addSubview:JCView1];
    JCView1.JCSignalTagColor=WhiteColor;
    [JCView1 setArrayTagWithLabelArray:skillArr];
    
    setViewHeight_Add(mainView,JCView1.frame.size.height+kmargin);
    
    UIView *skillLine=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,1)];
    [mainView addSubview:skillLine];
    skillLine.backgroundColor=MAIN_BACK_COLOR;
    setViewHeight_Add(mainView,1);
    
    // 简介
    UILabel *profileLabel=[mainView getLabelWithText:NSLocalizedString(@"Personal Introduction", nil) font:Font_Number(14) textColor:Color333333 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];//@"简介"
    [mainView addSubview:profileLabel];
    profileLabel.frame=CGRectMake(kmargin,mainView.frame.size.height+kmargin,APPW,30);
    setViewHeight_Add(mainView,30+kmargin);
    
    _profileView=[[UITextView alloc]initWithFrame:CGRectMake(kmargin,mainView.frame.size.height+kmargin/2,APPW-kmargin*2,kmargin*10)];
    _profileView.delegate=self;
    _profileView.backgroundColor=WhiteColor;
    _profileView.font=Font_Number(14);
    _profileView.textColor=ColorBDBDBD;
    ViewBorderRadius(_profileView,kmargin/5,1,MAIN_BACK_COLOR);
    [mainView addSubview:_profileView];
    setViewHeight_Add(mainView,kmargin*11);
    
     UILabel *tipProfileLabel=[mainView getLabelWithText:NSLocalizedString(@"EnterIntroduction", nil) font:Font_Number(14) textColor:ColorBDBDBD textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];//@"请填写您的简介"
    tipProfileLabel.tag=11000;
    [_profileView addSubview:tipProfileLabel];
    tipProfileLabel.frame=CGRectMake(kmargin,kmargin/2,APPW/2,30);
    
    
    UILabel *limitProfileLabel=[mainView getLabelWithText:[NSString stringWithFormat:@"0/%d",MaxTextInputValue] font:Font_Number(14) textColor:ColorBDBDBD textAlignment:NSTextAlignmentRight lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];
    [mainView addSubview:limitProfileLabel];
    limitProfileLabel.tag=666;
    [limitProfileLabel bringSubviewToFront:_profileView];
    limitProfileLabel.frame=CGRectMake(APPW-100-kmargin*2,mainView.frame.size.height-30,100,30);

    
    UIView *profileLine=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height+kmargin,APPW,1)];
    [mainView addSubview:profileLine];
    profileLine.backgroundColor=MAIN_BACK_COLOR;
    setViewHeight_Add(mainView,1+kmargin);
    
    // 执照号
    {
        _codeView=[[Label_TextField_Next alloc]initWithTitle:NSLocalizedString(@"Doc Medical License NO.", nil) placeholder:NSLocalizedString(@"Enter license no.", nil)];//initWithTitle:@"医生执照号" placeholder:@"请输入执照号"];
        _codeView.textField.keyboardType=UIKeyboardTypeNumberPad;
        _codeView.frame=CGRectMake(0,mainView.frame.size.height,APPW,kmargin*5);
        _codeView.textField.delegate=self;
        _codeView.textField.textAlignment=NSTextAlignmentRight;
        _codeView.backgroundColor=WhiteColor;
        [mainView addSubview:_codeView];
        UIView *codeViewLine=[UIView new];
        [_codeView addSubview:codeViewLine];
        codeViewLine.backgroundColor=MAIN_BACK_COLOR;
        [codeViewLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(APPW,1));
            make.bottom.mas_equalTo(0);
        }];
        
        setViewHeight_Add(mainView,kmargin*5);
    }
    
    //  上传封面图
   UILabel *uploadLabel=[mainView getLabelWithText:NSLocalizedString(@"Upload Medical License", nil) font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];//@"请上传您的医生执照"
    [mainView addSubview:uploadLabel];
    uploadLabel.frame=CGRectMake(kmargin,mainView.frame.size.height+kmargin,APPW/2,30);
    
    setViewHeight_Add(mainView,30+kmargin);
    
    
    UIButton *photoBtn=[[UIButton alloc]initWithFrame:CGRectMake(kmargin,mainView.frame.size.height+kmargin,160/2,160/2)];
    [mainView addSubview:photoBtn];
    [photoBtn addTarget:self action:@selector(photoBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    photoBtn.tag=200;
    [photoBtn setImage:[UIImage imageNamed:@"codePhoto"] forState:0];
    setViewHeight_Add(mainView,160/2+kmargin*2);
    
    
    UIButton *OKBtn=[[UIButton alloc]initWithFrame:CGRectMake(kmargin,mainView.frame.size.height+kmargin*2,APPW-kmargin*2,40)];
    [mainView addSubview:OKBtn];
    OKBtn.backgroundColor=Main_Color;
    OKBtn.titleLabel.font=Font_Number(16);
    ViewBorderRadius(OKBtn,kmargin/2,1,ClearColor);
    [OKBtn setTitle:NSLocalizedString(@"Done", nil) forState:UIControlStateNormal];//@"完成"
    [OKBtn setTitleColor:WhiteColor forState:UIControlStateNormal];
    [OKBtn addTarget:self action:@selector(OKBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    
    setViewHeight_Add(mainView,kmargin*8);
    
    return mainView;
}

-(void)addHospitalBtnEvent
{
    // 跳转到国家列表界面
    CountryListVC *countryListVC=[CountryListVC new];
    [self presentViewController:countryListVC animated:NO completion:nil];
}

// 选择从业时间
-(void)selectTimeBtnEvent
{
    // 选择从业年份
    JobFromYearVC *jobFromYearVC=[JobFromYearVC new];
    jobFromYearVC.delegate=self;
    [self presentViewController:jobFromYearVC animated:NO completion:nil];
}

// JobFromYearVCDelegate
-(void)jobFromYearListWithSelectIndex:(NSInteger)index yearName:(NSString *)yearName
{
    _selectTimeLabel.text=[NSString stringWithFormat:@"%@",yearName];
}

-(void)OKBtnEvent
{
    UIButton *yearsBtn=(UIButton *)[self.view viewWithTag:123];
    UIButton *sexBtn=(UIButton *)[self.view viewWithTag:124];
    
    if (_nameView.textField.text.length==0)
    {
        [self.view makeToast:@"姓名不能为空" duration:1.0 position:CSToastPositionCenter];
    }
    else  if (sexBtn.titleLabel.text.length==0)
    {
        [self.view makeToast:@"性别不能为空" duration:1.0 position:CSToastPositionCenter];
    }
    else  if (_emailView.textField.text.length==0)
    {
        [self.view makeToast:@"邮箱不能为空" duration:1.0 position:CSToastPositionCenter];
    }
    else  if (_profileView.text.length==0)
    {
        [self.view makeToast:@"简介不能为空" duration:1.0 position:CSToastPositionCenter];
    }
    else  if (_codeView.textField.text.length==0)
    {
        [self.view makeToast:@"医生执照号不能为空" duration:1.0 position:CSToastPositionCenter];
    }
    else
    {
        NSString *sexStr;   // NSLocalizedString(@"Female", nil)  NSLocalizedString(@"Male", nil)
        if ([sexBtn.titleLabel.text isEqualToString:NSLocalizedString(@"Female", nil)])
        {
            sexStr=@"0";
        }
        else if ([sexBtn.titleLabel.text isEqualToString:NSLocalizedString(@"Male", nil)])
        {
            sexStr=@"1";
        }
        else
        {
            [self.view makeToast:@"Gender must be male or female" duration:1.0 position:CSToastPositionCenter];
            return;
        }
        
        if (![[Util getUserDefaultObjectForKey:@"auth_status"] isEqualToString:@"0"]) {
            [self.view makeToast:@"你已经提交过资料" duration:1.0 position:CSToastPositionCenter];
            return;
        }
        
        NSDictionary *dic;
        // 判断所使用的语言
        BOOL isCN=[[LanguageConvert alloc] hasShownIsCnOrNot];
        // 中文
        if (isCN == YES)
        {
           dic=@{
                    @"doctor_id":[Util getUserDefaultObjectForKey:@"id"],
                    @"name":_nameView.textField.text,
                    @"garden":sexStr,
                    @"shop_id":_hospitalID,
                    @"document_number":_codeView.textField.text,
                    @"email":_emailView.textField.text,
                    @"title_cn":_selectTitleStr1,
                    @"speciality_cn":_selectTitleStr2,
                    @"congye_times":yearsBtn.titleLabel.text,
                    @"cn_descript":_profileView.text,
                    @"zid":_areaIDArr[0]
                                };
        }
        // 英文
        else if (isCN == NO)
        {
            dic=@{
                    @"doctor_id":[Util getUserDefaultObjectForKey:@"id"],
                    @"name":_nameView.textField.text,
                    @"garden":sexStr,
                    @"shop_id":_hospitalID,
                    @"document_number":_codeView.textField.text,
                    @"email":_emailView.textField.text,
                    @"title_en":_selectTitleStr1,
                    @"speciality_en":_selectTitleStr2,
                    @"congye_times":yearsBtn.titleLabel.text,
                    @"en_descript":_profileView.text,
                    @"zid":_areaIDArr[0]
                                };
        }
        
//        NSDictionary *dic=@{
//                            @"doctor_id":[Util getUserDefaultObjectForKey:@"id"],
//                            @"name":_nameView.textField.text,
//                            @"garden":sexStr,
//                            @"shop_id":_hospitalID,
//                            @"document_number":_codeView.textField.text,
//                            @"email":_emailView.textField.text,
//                            @"title_cn":_selectTitleStr1,
//                            @"speciality_cn":_selectTitleStr2,
//                            @"congye_times":yearsBtn.titleLabel.text,
//                            @"cn_descript":_profileView.text,
//                            @"zid":_areaIDArr[0]
//                            };

        __weak typeof(self) weakSelf=self;
        [QBRequest ConfirmWithDic:dic Success:^(id res)
         {
             
             if ([res[@"message"] isEqualToString:@"认证资料提交成功"]) {//.
                [self.view makeToast:NSLocalizedString(@"Upload Verification Succeed", nil) duration:1.0 position:CSToastPositionCenter];
             } else {
                [self.view makeToast:res[@"message"] duration:1.0 position:CSToastPositionCenter];
             }
            
             dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                 
                 // 跳转审核中界面
                 ReviewVC *reviewVC=[ReviewVC new];
                 reviewVC.auth_statusStr=@"1";
                 reviewVC.isConfirmType=@"isConfirmType";
                 [Util saveObjectToUserDefault:@"1" forKey:@"auth_status"];
                 [weakSelf presentViewController:reviewVC animated:NO completion:nil];
             });
             
         } failure:^(NSString *error)
         {
             
         }];
    }
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField
{

    if (_nameView.textField==textField)
    {
        [_nameView.textField resignFirstResponder];
    }

    if (_emailView.textField==textField)
    {
        [_emailView.textField resignFirstResponder];
    }
    if (_codeView.textField==textField)
    {
        [_codeView.textField resignFirstResponder];
    }
    
    return YES;
}


-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    return YES;
}

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    UILabel *profileLabel=(UILabel *)[self.view viewWithTag:11000];
    
    profileLabel.hidden=YES;
    return YES;
}

-(void)uploadHeadUrlBtnEvent
{
    SuPhotoPicker *picker=[[SuPhotoPicker alloc]init];
    picker.selectedCount=1;
    picker.preViewCount = 10;
    [picker showInSender:self handle:^(NSArray<UIImage *> *photos)
     {
         [QBRequest ConfirmInfoUserHeadUrlUploadWithImage:photos[0] userId:[Util getUserDefaultObjectForKey:@"id"] success:^(id res)
          {
              [self.view makeToast:res[@"message"] duration:1.0 position:CSToastPositionCenter];
              
              UIImageView *headURL=(UIImageView *)[self.view viewWithTag:100];
              [headURL sd_setImageWithURL:nil placeholderImage:photos[0]];
              
          } failure:^(NSString *error)
          {
              [self.view makeToast:error duration:1.0 position:CSToastPositionCenter];
          }];
     }];
}


-(void)photoBtnEvent
{
    SuPhotoPicker *picker=[[SuPhotoPicker alloc]init];
    picker.selectedCount=1;
    picker.preViewCount=10;
    
    [picker showInSender:self handle:^(NSArray<UIImage *> *photos){
        
         // UploadImageUserCodeUrlToService
         [QBRequest ConfirmInfoUserCodeUrlUploadWithImage:photos[0] userId:[Util getUserDefaultObjectForKey:@"id"] success:^(id res)
          {
              if ([res[@"message"] isEqualToString:@"医生执照上传成功"]) {//.
                  [self.view makeToast:NSLocalizedString(@"Upload medical license Succeed", nil) duration:1.0 position:CSToastPositionCenter];
              } else {
                  [self.view makeToast:res[@"message"] duration:1.0 position:CSToastPositionCenter];
              }
              
              UIButton *photoBtn=(UIButton *)[self.view viewWithTag:200];
              [photoBtn setImage:photos[0] forState:0];
              
          } failure:^(NSString *error)
          {
              [self.view makeToast:error duration:1.0 position:CSToastPositionCenter];
          }];
     }];
}


@end
